package com.bishe.psy.controller;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.*;
import com.bishe.psy.service.IArticleReplyService;
import com.bishe.psy.service.IArticleService;
import com.bishe.psy.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-21
 * Time: 10:17
 */

@Slf4j
@Api(tags = "文章回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService iArticleReplyService;

    @Resource
    private IArticleService iArticleService;

    @ApiOperation("回复帖子接口")
    @GetMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "文章id")  @RequestParam("articleId") Long articleId,
                            @ApiParam(value = "用户身份") @RequestParam("currentIdentity") Long currentIdentity,
                            @ApiParam(value = "回复内容") @RequestParam("content") String content) {
        //1.参数校验
        if(articleId == null || articleId <=0 || currentIdentity == null || currentIdentity <= 0
        || StringUtil.isEmpty(content)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
           return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //2.根据id查询当前文章信息
        Article article = iArticleService.selectByPrimaryKey(articleId);

        //3.判断该篇文章是否存在或者是否被删除,说明已经被删除
        if(article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //4.创建文章回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setContent(content);
        articleReply.setCurrentIdentity(currentIdentity);
        articleReply.setArticleId(articleId);

        //5.获取当前用户登录的信息,获取用户ID
        HttpSession session = request.getSession(false);
        if(currentIdentity == 1) {
            StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);
            articleReply.setPostUserId(stuUser.getId());

        } else if (currentIdentity == 2) {
            ConUser conUser = (ConUser) session.getAttribute(AppConfig.USER_SESSION);
            articleReply.setPostUserId(conUser.getId());
        }else {
            AdmUser admUser = (AdmUser) session.getAttribute(AppConfig.USER_SESSION);
            articleReply.setPostUserId(admUser.getId());
        }

        //4.调用service;
        iArticleReplyService.create(articleReply);

        //5.返回数据
        return AppResult.success();

    }


    @ApiOperation("查看某篇文章的所有回复记录接口")
    @GetMapping("/select")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam(value ="文章id") @RequestParam("articleId") Long articleId) {

        //1.参数校验
        if(articleId == null || articleId <= 0) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //2.查询该篇文章是否存在
        Article article = iArticleService.selectByPrimaryKey(articleId);
        if(article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //3.调用service
        List<ArticleReply> articleReplies = iArticleReplyService.selectArticleReplyById(articleId);

        //4.返回数据
        return AppResult.success(articleReplies);

    }
}
